<?php

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use function App\Webdisk\makeRes;

/*
|--------------------------------------------------------------------------
| API Routes
|--------------------------------------------------------------------------
|
| Here is where you can register API routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| is assigned the "api" middleware group. Enjoy building your API!
|
*/

Route::middleware('auth:api')->get('/user', function (Request $request) {
    return $request->user();
});

$noLogin = [
    'User'=>['login'],
    'Contribute'=>['upload'],
    'Resource'=>['list','search'],
    'Media' => ['getPubliclist', 'getListContent']
];


Route::middleware(['checkLogin','checkPlaylistOwner'])->group(function(){
    $playlist = [
        'addToList',
        'removeFromList',
        'removePlaylist'
    ];
    foreach ($playlist as $method) {
        Route::any("Media/$method", "Webdisk\\MediaController@$method");
    }
});

foreach ($noLogin as $m => $as) {
    foreach ($as as $key => $a) {
        Route::any("$m/$a","Webdisk\\{$m}Controller@$a");
    }
}

Route::middleware(['checkLogin'])->group(function () {
    Route::any('{module}/{action}', function ($model,$action) {
        $class = "\\App\\Http\\Controllers\\Webdisk\\{$model}Controller";
        if(class_exists($class) && method_exists($class,$action)){
            $obj = \App::make($class);
            $ret = \App::call([$obj,$action]);
            return $ret;
        }else{
            return response('NOT FOUND',404);
        }
    });
});



Route::middleware(['checkLogin','authAdmin'])->prefix("admin")->group(function(){
    Route::any('{module}/{action}', function ($model,$action) {
        $class = "\\App\\Http\\Controllers\\Webdisk\\Admin\\{$model}Controller";
        if(class_exists($class) && method_exists($class,$action)){
            $obj = \App::make($class);
            $ret = \App::call([$obj,$action]);
            return $ret;
        }else{
            return response(makeRes(-1,null,'API NOT FOUND'),404);
        }
    });
});
